package com.example.sekerim;

import java.io.IOException;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi") public class HomePageActivity extends FragmentActivity {

	private CharSequence mTitle;
	private int sectionNumber=-1;
	private String[] mMenuTitles = Constants.homepage_left_menu;
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;

	private Patient patient = new Patient();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_page);

		initializePageElements();
		selectItem(0);

		try {
			patient = (Patient) InternalStorage.readObject(this, Constants.shared_info);
		} catch (ClassNotFoundException e) {
			Log.e("While reading", e.getMessage());
		} catch (IOException e) {
			Log.e("While reading", e.getMessage());
		}

	}

	public void initializePageElements()
	{

		mMenuTitles = Constants.homepage_left_menu;
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this,
				R.layout.drawer_list_item, mMenuTitles));
		// Set the list's click listener
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());


	}

	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements ListView.OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			selectItem(position);
			sectionNumber = position;
		}


	}

	private void selectItem(int position) {
		// Create a new fragment and specify the planet to show based on position

		Fragment fragment = null;

		switch(position)
		{
		case 0:

			fragment = new HomePageFragment();
			break;
		case 1:
			fragment = new FoodSelectionFragment();
			break;
		case 2:
			fragment = new ExerciseDisplayFragment();
			break;
		default:
			fragment = new HomePageFragment();
			break;

		}
		invalidateOptionsMenu();
		// Insert the fragment by replacing any existing fragment
		FragmentManager fragmentManager = getSupportFragmentManager();
		fragmentManager.beginTransaction()
		.replace(R.id.content_frame, fragment)
		.commit();



		mDrawerList.setItemChecked(position, true);
		setTitle(mMenuTitles[position]);
		mDrawerLayout.closeDrawer(mDrawerList);
	}

	/*
	@SuppressLint("ValidFragment") public class HomePageFragment extends Fragment {

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup parent,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.fragment_home_page, parent, false);
			mTextView = (TextView) v.findViewById(R.id.edit_text);

			String s = SaveSharedPreference.getMailAddress(HomePageActivity.this) + "-" +
					patient.getPassword() + "-" +
					patient.getUserName() + "-" +
					patient.getUserSurname() + "-" +
					"isMale:" + patient.isMale() + "-" +
					patient.getBirthdayDay() + "/" + patient.getBirthdayMonth() + "/" + patient.getBirthdayYear()
					+ patient.getEthnicity();
			mTextView.setText(s);
			return v;
		}
	}
	 */

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	@Override
	public boolean onPrepareOptionsMenu (Menu menu) {

		menu.clear();

		if (sectionNumber == 1)
		{
			getMenuInflater().inflate(R.menu.activity_home_page_food_addition_menu_items, menu);
		}
		else if (sectionNumber == 2)
		{ 
			getMenuInflater().inflate(R.menu.activity_home_page_exercise_addition_menu_items, menu);
		}
		else
		{
			getMenuInflater().inflate(R.menu.main, menu);
		}


		return super.onPrepareOptionsMenu(menu);
	}

	/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_home_page_action_bar_items, menu);
		return super.onCreateOptionsMenu(menu);
	}
	 */

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_add_exercise:

			Intent i = new Intent(HomePageActivity.this, ExerciseAddActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_add_food:

			Intent i2 = new Intent(HomePageActivity.this, RegistrationActivityAccountInformation.class);
			startActivity(i2);
			return true;
		}
		return false;
	}


}
